<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">

      <el-form-item>
        <el-input v-model="searchObj.orderId" placeholder="订单号"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.userId" placeholder="用户id"/>
      </el-form-item>

      <el-form-item>
        <el-input v-model="searchObj.status" placeholder="订单状态"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- banner列表 -->
    <el-table v-loading="listLoading" :data="list"
              border
              fit
              highlight-current-row>

      <el-table-column prop="orderId" label="订单号"/>
      <el-table-column prop="userId" label="用户id"/>
      <el-table-column prop="bookTime" label="预定时间"/>
      <el-table-column prop="bookDays" label="预定天数"/>
      <el-table-column prop="total" label="总价"/>
      <el-table-column prop="createTime" label="创建时间"/>

      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" size="mini">未入住</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success" size="mini">已入住</el-tag>
          <el-tag v-if="scope.row.status == 2" type="info" size="mini">已完成</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="seleHotelOrder(scope.row)">查看</el-button>
          <el-button type="danger" size="mini" @click="removeHotelOrder(scope.row)">移除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />

    <el-dialog
      title="订单详情"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose">

      <el-descriptions class="margin-top" title="订单详情" :column="3" :size="size" border direction="vertical">

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            订单号
          </template>
          {{order.orderId}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            用户id
          </template>
          {{order.userId}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            联系电话
          </template>
          {{order.phone}}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-watch"></i>
            预定时间
          </template>
          {{order.bookTime}}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-watch"></i>
            预定天数
          </template>
          {{order.bookDays}}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            <i></i>
            总价
          </template>
          {{order.total}}
        </el-descriptions-item>

        <el-descriptions-item label="酒店名称" content-class-name="my-content">
          {{order.hotel.zkname}}
        </el-descriptions-item>

        <el-descriptions-item span="2" content-class-name="my-content">
          <template slot="label">
            <i></i>
            酒店简介
          </template>
          {{order.hotel.intro}}
        </el-descriptions-item>

        <el-descriptions-item content-class-name="my-label">
          <template slot="label">
            <i></i>
            房间编号
          </template>
          {{order.room.roomcode}}
        </el-descriptions-item>

        <el-descriptions-item content-class-name="my-label">
          <template slot="label">
            <i></i>
            房间名称
          </template>
          {{order.room.roomname}}
        </el-descriptions-item>

        <el-descriptions-item content-class-name="my-label">
          <template slot="label">
            <i></i>
            房间价格
          </template>
          {{order.room.roomprice}}
        </el-descriptions-item>

      </el-descriptions>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "hotel-order",
  data(){
    return{
      list: null, // 订单列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      listLoading: true, // 数据是否正在加载
      dialogVisible: false,
      searchObj: {
        orderId: null,
        userId: null,
        status: null   //查询未处理订单
      },
      order: {
        orderId: null,
        userId: null,
        hotel: {},
        room: {},
        bookTime: null,
        bookDays: null,
        phone: null,
        idcard: null,
        total: null,
        status: null,
        createTime: null,
        updateTime: null
      },
      size: '',
    }
  },
  methods:{
    seleHotelOrder(order){
      console.log(order)
      this.order = order
      this.dialogVisible = true
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          console.log("close.....................")
          this.order = {
            orderId: null,
            userId: null,
            hotel: {},
            room: {},
            bookTime: null,
            bookDays: null,
            phone: null,
            idcard: null,
            total: null,
            status: null,
            createTime: null,
            updateTime: null
          }
          done();
        })
        .catch(_ => {});
    },
    fetchData(page = 1){
      this.page = page
      this.$http({
        url: this.$http.adornUrl(`/order/hotel/list/${page}/${this.limit}`),
        method: 'get',
        params: this.searchObj
      }).then(({data}) => {
        this.list = data.data.records
        this.total = data.data.total
        this.listLoading = false
      })
    },
    // 重置查询表单
    resetData() {
      this.searchObj = {}
      this.searchObj.status=0
      this.fetchData()
    },
    // 页码切换
    changeSize(val) {
      this.limit = val
      this.page = 1
      this.fetchData(1)
    },
    removeHotelOrder(order){
      this.$confirm(`确定对  ${order.orderId}  删除？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/order/hotel/deleteById/'),
          method: 'post',
          data: this.$http.adornData(order.orderId, false)
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.fetchData(this.page)
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {
      })
    },
  },
  created() {
    this.fetchData()
  },
}
</script>

<style>

  .my-label {
    background: #E1F3D8;
  }

  .my-content {
    background: #FDE2E2;
  }
</style>
